package com.project.myxima.data;

import com.project.myxima.utils.Constants;
import com.ximalaya.ting.android.opensdk.constants.DTransferConstants;
import com.ximalaya.ting.android.opensdk.datatrasfer.CommonRequest;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.GussLikeAlbumList;
import com.ximalaya.ting.android.opensdk.model.album.SearchAlbumList;
import com.ximalaya.ting.android.opensdk.model.track.TrackList;
import com.ximalaya.ting.android.opensdk.model.word.HotWordList;
import com.ximalaya.ting.android.opensdk.model.word.SuggestWords;

import java.util.HashMap;
import java.util.Map;

public class XimalayaApi {

    /**
     * SingleInstance
     */
    private static XimalayaApi sXimalayaApi;
    private XimalayaApi(){ }

    public static XimalayaApi getXimalayaApi(){
        if (sXimalayaApi == null) {
            synchronized (XimalayaApi.class){
                if(sXimalayaApi == null){
                    sXimalayaApi = new XimalayaApi();
                }
            }
        }
        return sXimalayaApi;
    }





    /**
     * refactor code from RecommendPresenter
     */
    public void getRecommendList(IDataCallBack<GussLikeAlbumList> callBack) {

        //封装要传给它的参数
        Map<String, String> map = new HashMap<>();
        //这个参数表示一页该返回多少条数据
        map.put(DTransferConstants.LIKE_COUNT, Constants.COUNT_RECOMMEND + "");

        CommonRequest.getGuessLikeAlbum(map, callBack);//TODO:What is CommonRequest?
    }

    /**
     * refactor code from AlbumDetailPresenter
     */
    public void getAlbumDetail(IDataCallBack<TrackList> callBack, long albumId, int pageIndex){
        Map<String, String> map = new HashMap<>();
        map.put(DTransferConstants.ALBUM_ID, albumId + "");
        map.put(DTransferConstants.SORT, "asc");
        map.put(DTransferConstants.PAGE, pageIndex + "");
        map.put(DTransferConstants.PAGE_SIZE, Constants.COUNT_DEFAULT + "");

        //Request data
        CommonRequest.getTracks(map,callBack);

    }

    /**
     * search with a keyword
     * @param keyword
     */
    public void searchByKeyword(String keyword, int page, IDataCallBack<SearchAlbumList> iDataCallBack) {
        Map<String,String> map = new HashMap<>();
        map.put(DTransferConstants.SEARCH_KEY, keyword);
        map.put(DTransferConstants.PAGE, page + "");
        map.put(DTransferConstants.PAGE_SIZE,Constants.COUNT_DEFAULT + "");

        CommonRequest.getSearchedAlbums(map,iDataCallBack);
    }

    public void getHotWord(IDataCallBack<HotWordList> callback) {
        Map<String, String> map = new HashMap<String, String>();
        map.put(DTransferConstants.TOP, Constants.COUNT_HOTWORD  +"");
        CommonRequest.getHotWords(map, callback);
    }

    public void getSuggestionWord(String word, IDataCallBack<SuggestWords> callback) {
        Map<String, String> map = new HashMap<String, String>();
        map.put(DTransferConstants.SEARCH_KEY, word);
        CommonRequest.getSuggestWord(map, callback);
    }
}
